#!/usr/bin/python

import subprocess
import sys
import socket

import rrd_net
import rrd_fs
import rrd_memory
import rrd_uptime
import rrd_loadavg
import rrd_cpuusage
import rrd_tasks
import rrd_ping
import rrd_hosts
import rrd_duply
import rrd_weather
import rrd_s3du
import rrd_cputemp

import where
import emit_html
import verbose

verbose.initialize(sys.argv)

(atwork, atlaptop, atnas) = where.whereami()

if verbose.verbose:
    print("Collecting top data")
top = subprocess.Popen(["/usr/bin/top", "-bn10", "-d0.5"], shell=False, stdout=subprocess.PIPE)
top_lines = top.communicate()[0].split("\n")

objects = []

objects.append(rrd_ping.RrdPing(domain="NAS", name="ping"))
objects.append(rrd_net.RrdNetLocal(domain="NAS", name="eth0", alt=""))
objects.append(rrd_memory.RrdMemoryLocal(domain="NAS"))
objects.append(rrd_uptime.RrdUptimeLocal(domain="NAS"))
objects.append(rrd_loadavg.RrdLoadavgLocal(domain="NAS"))
objects.append(rrd_cpuusage.RrdCpuUsage(domain="NAS", top_lines=top_lines))
objects.append(rrd_tasks.RrdTasks(domain="NAS", top_lines=top_lines))

# TODO: VOIP

if atlaptop == True or atnas == True:
    objects.append(rrd_hosts.RrdHosts(domain="NAS"))
    objects.append(rrd_net.RrdNetSnmp(domain="router", name="vlan1", alt="(WAN)"))
    objects.append(rrd_net.RrdNetSnmp(domain="router", name="vlan0", alt="(Internal Wired)"))
    objects.append(rrd_net.RrdNetSnmp(domain="router", name="br0", alt="(All Local)"))
    objects.append(rrd_net.RrdNetSnmp(domain="router", name="eth1", alt="(Wireless)"))
    objects.append(rrd_memory.RrdMemorySnmp(domain="router"))
    objects.append(rrd_uptime.RrdUptimeSnmp(domain="router"))
    objects.append(rrd_loadavg.RrdLoadavgSnmp(domain="router"))
    objects.append(rrd_s3du.RrdS3Du(domain="NAS", name="AWS-S3"))

if atlaptop == True:
    objects.append(rrd_duply.RrdDuplyCmd(domain="Laptop", base="/home/dan/SVN/duply", name="daily"))
    objects.append(rrd_duply.RrdDuplyCmd(domain="Laptop", base="/home/dan/SVN/duply", name="critical"))
    objects.append(rrd_fs.RrdFS(domain="Laptop", name="home", mountpoint="/home"))
    objects.append(rrd_fs.RrdFS(domain="Laptop", name="rootfs", mountpoint="/"))

if atnas == True:
    objects.append(rrd_weather.RrdWeather(domain="All", name="RDU"))
    objects.append(rrd_duply.RrdDuplyLocal(domain="Laptop", base="/mnt/flash/NAS/Backup/laptop", name="daily"))
    objects.append(rrd_duply.RrdDuplyLocal(domain="Laptop", base="/mnt/flash/NAS/Backup/laptop", name="critical"))
    objects.append(rrd_duply.RrdDuplyCmd(domain="NAS", base="/mnt/flash/local/duply", name="critical"))
    objects.append(rrd_fs.RrdFS(domain="NAS", name="vg_pi-flash", mountpoint="/mnt/flash"))
    objects.append(rrd_fs.RrdFS(domain="NAS", name="rootfs", mountpoint="/"))
    objects.append(rrd_cputemp.RrdCpuTempSys(domain="NAS", sensor_file="/sys/class/thermal/thermal_zone0/temp", name="core", limit=85.0))

if atwork == True:
    objects.append(rrd_duply.RrdDuplyLocal(domain="Work", base="/mnt/extreme/bear/parent/dschudel/Backup", name="daily"))
    objects.append(rrd_duply.RrdDuplyCmd(domain="Work",   base="/home/dschudel/bin/duply", name="critical"))
    objects.append(rrd_fs.RrdFS(domain="Work", name="whole-root", mountpoint="/dev/mapper/whole-root"))
    objects.append(rrd_fs.RrdFS(domain="Work", name="whole-home", mountpoint="/dev/mapper/whole-home"))
    objects.append(rrd_cputemp.RrdCpuTempLmSensors(domain="Work", sensor_name="temp2"))

for object in objects:
    object.conditional_update()

has_one_updated = False
for object in objects:
    if object.conditional_graph():
        has_one_updated = True

if True or has_one_updated:
    emit_html.doit(objects)
